package com.chookss.home.interview;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.WebCommonActivity;
import com.chookss.base.StaticClass;
import com.chookss.base.Token;
import com.chookss.home.entity.InterviewPjEntity;
import com.chookss.home.entity.InterviewRecordsEntity;
import com.chookss.tiku.FeedbackActivity;
import com.chookss.tools.ClearMoreZeroUtil;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.NavigationTools;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.view.CircleImageView;
import com.google.gson.reflect.TypeToken;
import com.johnrambo.ktea.ktExtends.ShareUtils;
import com.lvgroup.hospital.base.BaseAct;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//答题结果页
public class InterviewExamResultActivity extends BaseAct {

    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.tvDate)
    TextView tvDate;
    @BindView(R.id.spaceView)
    View spaceView;
    @BindView(R.id.tvScoreInit1)
    TextView tvScoreInit1;
    @BindView(R.id.tvScore)
    TextView tvScore;
    @BindView(R.id.tvScoreInit2)
    TextView tvScoreInit2;
    @BindView(R.id.tvAverage)
    TextView tvAverage;
    @BindView(R.id.tvAverageInit)
    TextView tvAverageInit;
    @BindView(R.id.llScore)
    LinearLayout llScore;
    @BindView(R.id.tvPractice)
    TextView tvPractice;
    @BindView(R.id.tvPracticeNum)
    TextView tvPracticeNum;
    @BindView(R.id.tvPracticeInit)
    TextView tvPracticeInit;
    @BindView(R.id.tvTime)
    TextView tvTime;
    @BindView(R.id.tvTimeInit)
    TextView tvTimeInit;
    @BindView(R.id.tvDayout)
    TextView tvDayout;
    @BindView(R.id.llBottom)
    LinearLayout llBottom;
    @BindView(R.id.tvBottomInit)
    TextView tvBottomInit;
    @BindView(R.id.knowledgeRcv)
    RecyclerView knowledgeRcv;
    @BindView(R.id.llBg)
    LinearLayout llBg;
    @BindView(R.id.rlBg)
    RelativeLayout rlBg;
    private PjAdapter adapter;
    private List<InterviewPjEntity> list = new ArrayList<>();
    private int type = 0;//0和1只有记录2需要评价
    private InterviewRecordsEntity interviewRecordsEntity;
    private String recordUid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interview_exam_result);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        type = getIntent().getIntExtra("type", 0);
        interviewRecordsEntity = (InterviewRecordsEntity) getIntent().getSerializableExtra("interviewResult");
        if (interviewRecordsEntity == null) {
            finish();
            return;
        }
        recordUid=interviewRecordsEntity.getRecordUid();
        Drawable drawableRight = ContextCompat.getDrawable(this, R.drawable.icon_z_wenh);
        drawableRight.setBounds(0, 0, Utils.dip2px(this, 13), Utils.dip2px(this, 13));
        ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) rlBg.getLayoutParams();
        params.height = 743 * Utils.getScreenWidthPixels(this) / 828;
        rlBg.setLayoutParams(params);
        llBg.setBackgroundResource(R.drawable.zqv_bg);
        llScore.setVisibility(View.VISIBLE);
        knowledgeRcv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new PjAdapter(R.layout.item_interview_pj, this, list);
        knowledgeRcv.setAdapter(adapter);
        getExamData();
        getData();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getExamData();
        getData();
    }

    private void getExamData() {
        showLoading();
        Map<String, String> map = new HashMap<>();
        map.put("recordUid", recordUid);
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<InterviewRecordsEntity>>() {
        }.getType(), this, Urls.getInterviewByRecordUid, SignUtils2.makeSignStr(map), false, new ObserverCallback<InterviewRecordsEntity>() {
            @Override
            public void onSuccess(InterviewRecordsEntity data) {
                dismissLoading();
                if(commonTitleTxt==null){
                    return;
                }
                commonTitleTxt.setText(data.getExamName());
                tvScoreInit1.setText("得分");
                tvScoreInit2.setText("分");
                tvPracticeInit.setText("答对题数");
                tvAverageInit.setText("成绩");
                int seconds = Utils.changeNumber(data.getTimeLength());
                tvDate.setText("答题时间：" + data.getCreateTime());
                tvScore.setText(ClearMoreZeroUtil.subZeroAndDot(data.getScore()));
                if (seconds < 60) {
                    tvTime.setText(seconds + "");
                    tvTimeInit.setText("秒");
                } else {
                    tvTime.setText((seconds / 60) + "");
                }
                tvPractice.setText((Utils.changeNumber(data.getRightNum())) + "");
                tvPracticeNum.setText("/" + Utils.changeNumber(data.getExamTotalNum()) + "道");
                if (type == 2) {
                    if (data.getDayCount().equals("1")) {
                        llBottom.setVisibility(View.GONE);
                        tvBottomInit.setVisibility(View.GONE);
                        tvDayout.setVisibility(View.GONE);
                    } else {
                        llBottom.setVisibility(View.VISIBLE);
                        tvBottomInit.setVisibility(View.VISIBLE);
                        tvDayout.setVisibility(View.VISIBLE);
                    }
                } else {
                    llBottom.setVisibility(View.GONE);
                    tvDayout.setVisibility(View.GONE);
    //            spaceView.setVisibility(View.GONE);
                    tvBottomInit.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(String errorMsg) {
                dismissLoading();
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }
    private void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("companyCode", new ShareUtils().getString(this, StaticClass.COMPANYCODE, ""));
        map.put("personCode", interviewRecordsEntity.getEmployeeCode());
        map.put("examCode", interviewRecordsEntity.getExamCode());
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<List<InterviewPjEntity>>>() {
        }.getType(), this, Urls.selectHrReultList, SignUtils2.makeSignStr(map), false, new ObserverCallback<List<InterviewPjEntity>>() {
            @Override
            public void onSuccess(List<InterviewPjEntity> data) {
                list.clear();
                if (data != null) {
                    list.addAll(data);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }

    class PjAdapter extends BaseQuickAdapter<InterviewPjEntity, BaseViewHolder> {
        private Context context;

        public PjAdapter(int layoutResId, Context context, @io.reactivex.annotations.Nullable List<InterviewPjEntity> data) {
            super(layoutResId, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolder helper, InterviewPjEntity item) {
            CircleImageView ivHead = helper.getView(R.id.ivHead);
            TextView tvName = helper.getView(R.id.tvName);
            ImageView ivType = helper.getView(R.id.ivType);
            TextView tvContent = helper.getView(R.id.tvContent);
            if (!Utils.isNull(item.getHeadPhotoThumbUrl())) {
                GlideUtils.loadAvatar(InterviewExamResultActivity.this, item.getHeadPhotoThumbUrl(), ivHead);
            } else {
                GlideUtils.loadAvatar(InterviewExamResultActivity.this, item.getHeadPhotoUrl(), ivHead);

            }
            if (Utils.isNull(item.getPostName())) {
                tvName.setText(item.getEmployeeName());
            } else {
                tvName.setText(item.getEmployeeName() + " (" + item.getPostName() + ")");
            }
            tvContent.setText(item.getInterviewEvaluate());
            if (Utils.isNull(item.getPass())) {
                if ("1".equals(item.getDayCount())) {
                    ivType.setBackgroundResource(R.drawable.icon_timeout_30);
                } else {
                    ivType.setBackgroundResource(R.drawable.icon_no_pj_30);
                }
            } else {
                if ("1".equals(item.getPass())) {
                    ivType.setBackgroundResource(R.drawable.iv_pass_30);
                } else {
                    ivType.setBackgroundResource(R.drawable.iv_no_pass_30);
                }
            }
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MyEvent messageEvent) {
        //事件订阅者处理事件
        if (messageEvent.getHashMap() != null) {
            Map<String, String> map = messageEvent.getHashMap();
        } else {
            String msg = messageEvent.getMessage();
            if (Utils.isNull(msg)) {
                return;
            }
            if (msg.equals("updateInterView_result")) {
                llBottom.setVisibility(View.GONE);
                tvBottomInit.setVisibility(View.INVISIBLE);
                getData();
            }
        }
    }

    @OnClick({R.id.common_title_back, R.id.tvPass, R.id.tvLook, R.id.tvNoPass})
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.tvLook:
                intent = new Intent(this, WebCommonActivity.class);
                String url = NavigationTools.getAllUrl(this, Urls.indexAnswer);
                String companyCode = new ShareUtils().getString(this, StaticClass.COMPANYCODE, "");
                url = url +"&token="+ Token.INSTANCE.getToken() + "&examCode=" + interviewRecordsEntity.getExamCode() + "&companyCode=" + companyCode + "&personCode=" + interviewRecordsEntity.getEmployeeCode() + "&createTime=" + Utils.getAccurateDate2(interviewRecordsEntity.getCreateTime(), 0, 10) + "&recordUid=" + interviewRecordsEntity.getRecordUid() + "&totalNum=" + ClearMoreZeroUtil.subZeroAndDot(interviewRecordsEntity.getExamTotalNum());
                intent.putExtra("url", url);
                intent.putExtra("title", interviewRecordsEntity.getExamName());
                intent.putExtra("waterMark", "16");
                startActivity(intent);
                break;
            case R.id.tvPass:
                intent = new Intent(this, FeedbackActivity.class);
                intent.putExtra("pass", 1);
                intent.putExtra("type", 2);
                intent.putExtra("personCode", interviewRecordsEntity.getEmployeeCode());
                startActivity(intent);
                break;
            case R.id.tvNoPass:
                intent = new Intent(this, FeedbackActivity.class);
                intent.putExtra("pass", 0);
                intent.putExtra("type", 2);
                intent.putExtra("personCode", interviewRecordsEntity.getEmployeeCode());
                startActivity(intent);
                break;
        }
    }

}
